package lib.bone.access.module.dict.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.access.module.dict.dao.NepuSubjectDictDAO;
import lib.bone.access.module.dict.entity.NepuSubjectDict;
import lib.bone.access.module.dict.DictService;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * cg
 */
@Slf4j
@Service
public class SubjectDictServiceImpl implements DictService<NepuSubjectDict,String> {

    @Autowired
    NepuSubjectDictDAO nepuSubjectDictDAO;

//    @Autowired
//    NepuSubjectDAO subjectDAO;

    @Override
    public String getServiceType() {
        return ResearchTypeConst.SUBJECT;
    }

    public Optional<NepuSubjectDict> calcDict(String type,Double funds){

        return nepuSubjectDictDAO.findBySubjectTypeAndFundsFloorLessThanEqualAndFundsCeilingGreaterThanEqual(type,funds,funds);

    }

    @Override
    public PageResultDTO<NepuSubjectDict> getAll(Pageable pageable, Example<NepuSubjectDict> example) {
        Page<NepuSubjectDict> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = nepuSubjectDictDAO.findAll(pageable);
        }else {
            page = nepuSubjectDictDAO.findAll(example,pageable);
        }
        List<NepuSubjectDict> List = page.getContent();
        PageResultDTO<NepuSubjectDict> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(List);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO<NepuSubjectDict> deleteById(String id) {
//        Boolean useExist = subjectDAO.existsBySubjectDictId(id);
//        if(useExist){
//            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"存在关联该横向课题的信息,拒绝执行删除!");
//        }
        // 2. 判断资源内容是否存在
        Optional<NepuSubjectDict> nepuSubjectDictOp = getById(id);
        if(!nepuSubjectDictOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"未找到ID:["+id+"]对应资源!");
        nepuSubjectDictDAO.deleteById(id);
        return ResponseDTO.succMsg("删除成功!");
    }

    @Override
    public Optional<NepuSubjectDict> getById(String id) {
        return nepuSubjectDictDAO.findById(id);
    }

    @Override
    public NepuSubjectDict saveOne(NepuSubjectDict nepuSubjectDict) {
        if (Validator.isNotEmpty(nepuSubjectDict.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuSubjectDictDAO.save(nepuSubjectDict);
    }

    @Override
    public NepuSubjectDict updateOne(NepuSubjectDict nepuSubjectDict) {
        if (Validator.isEmpty(nepuSubjectDict.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuSubjectDictDAO.save(nepuSubjectDict);
    }
}
